package org.darkforest.merchant.ajaxTemplete.impl;

import org.darkforest.enums.AjaxAlbumTypeEnum;
import org.darkforest.merchant.ajaxTemplete.AlbumAjaxPage;
import org.darkforest.model.AlbumPic;
import org.darkforest.model.AlbumPicPageBean;

/**
 * Created by valentina on 15/3/19.
 * 添加商品第三步商品相册返回code
 */
public class GoodsStep3GalleryAlbum extends AlbumAjaxPageTemplete implements AlbumAjaxPage {

    public GoodsStep3GalleryAlbum(){
        this.insertType = AjaxAlbumTypeEnum.GALLERY.getText();
    }

    @Override
    protected String getMainDiv(Integer specSize) {
        String tpl = "<div class='goods-gallery' nctype='gallery-"+specSize+"'> " +
                "<a class='sample_demo' href='"+basePath+"/album/ajaxPicList?insertType=gallery&specSize="+specSize+"' style='display:none;'>提交</a>";
        return tpl;
    }

    @Override
    protected String getPicListCode(AlbumPicPageBean picPage,Integer specSize) {
        String tpl ="";
        if(picPage.getResultList()!=null && picPage.getResultList().size()>0){
            tpl +=  "<ul class='list'>";
            //循环出图片信息 js需要用双引号否则不执行
            for (AlbumPic pic:picPage.getResultList()){
                tpl +=   "      <li onclick=\"insert_img(";
                //主图以及移动端插入360px的缩略图
                tpl +=   "'"+pic.getApicCover()+"',";
                tpl +=   "'"+aliyun_path+"/"+pic.getApicCover()+"',"+specSize+");\">" ;
                tpl +=   "           <a href='JavaScript:void(0);'><img src='"+aliyun_path+"/60_"+pic.getApicCover()+"' title='"+pic.getApicName()+"'/></a></li>" ;
            }
            tpl +=  "</ul>";
        }else{
            tpl += "<div class='warning-option'><i class='icon-warning-sign'></i><span>相册中暂无图片</span></div>";
        }
        return tpl;
    }

    @Override
    protected String getScriptCode() {
        String tpl = "";
        tpl += "<script type=\"text/javascript\">";
        tpl += "$(document).ready(function(){";

        tpl += "    $('div[nctype=\"gallery-"+this.specSize+"\"]').find('.demo').unbind().ajaxContent({" +
                "       event:'click'," +
                "       loaderType:'img'," +
                "       loadingMsg:'" + basePath + "/resources/images/loading.gif'," +
                "       target:'div[nctype=\"album-"+this.specSize+"\"]'" +
                "   });" +
                "   $('div[nctype=\"gallery-"+this.specSize+"\"]').find('select[name=\"jumpMenu\"]').unbind().change(function(){" +
                "       var $_select_submit = $('div[nctype=\"gallery-"+this.specSize+"\"]').find('.sample_demo');" +
                "       var $_href = $_select_submit.attr('href') + \"&albumId=\" + $(this).val();" +
                "       $_select_submit.attr('href',$_href);" +
                "       $_select_submit.unbind().ajaxContent({" +
                "           event:'click'," +
                "           loaderType:'img'," +
                "           loadingMsg:'" + basePath + "/resources/images/loading.gif'," +
                "           target:'div[nctype=\"album-"+this.specSize+"\"]'" +
                "       });" +
                "       $_select_submit.click()" +
                "   });";

        tpl += "});" +
                "</script>";
        return tpl;
    }
}
